package day2018_9_16;

import java.io.FileInputStream;
import java.util.Properties;

public class Factory {
    public static Weapon getWeapon(){

        String className = getClassName();

        Weapon weapon = null;

        try {
            // 通过反射，获取类名对应的字节码对象
            Class clazz = Class.forName(className);
            // 通过类的字节码生成类的对象
            weapon = (Weapon) clazz.newInstance();

        } catch (Exception e) {

            e.printStackTrace();
        }

        return weapon;
    }

    /**
     * 获取配置文件中的类名
     */
    private static String getClassName() {

        String className = null;

        try {
            Properties prop = new Properties();

            FileInputStream fis = new FileInputStream("1606101007邱义成\\src\\main\\resources\\Weapon.properties");

            prop.load(fis);

            className = prop.getProperty("className");

            return className;

        } catch (Exception e) {

            e.printStackTrace();
        }

        return className;
    }

}
